package com.lwj.service.configuration;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lwj.dto.configuration.DepartmentRoleDto;
import com.lwj.dto.configuration.RoleListDto;
import com.lwj.mapper.configuration.DepartmentRoleMapper;
import com.lwj.model.administration.Department;
import com.lwj.model.configuration.DepartmentRole;
import com.lwj.service.DozerService;
import com.lwj.service.administration.DepartmentService;
import com.lwj.vo.PageBean;
import com.lwj.vo.configuration.PermissionCheckVo;
import com.lwj.vo.configuration.RoleVo;
import com.lwj.vo.system.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DepartmentRoleServiceImpl implements DepartmentRoleService {
    @Autowired
    private DepartmentRoleMapper departmentRoleMapper;
    @Autowired
    private DozerService dozerService;
    @Autowired
    private DepartmentService departmentService;

    @Override
    public int deleteByPrimaryKey(String departmentRoleId) {
        return departmentRoleMapper.deleteByPrimaryKey(departmentRoleId);
    }

    @Override
    public int insert(DepartmentRole record) {
        return departmentRoleMapper.insert(record);
    }

    @Override
    public int insertSelective(DepartmentRole record) {
        return departmentRoleMapper.insertSelective(record);
    }

    @Override
    public DepartmentRole selectByPrimaryKey(String departmentRoleId) {
        return departmentRoleMapper.selectByPrimaryKey(departmentRoleId);
    }

    @Override
    public int updateByPrimaryKeySelective(DepartmentRole record) {
        return departmentRoleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DepartmentRole record) {
        return departmentRoleMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageBean<RoleVo> roleList(RoleListDto dto) {
        Department department = departmentService.selectByPrimaryKey(dto.getDepartmentId());
        dto.setPath(department.getPath());
        Page<RoleVo> objects = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize());
        departmentRoleMapper.roleList(dto);
        return PageBean.page(objects);
    }

    @Override
    public String roleSave(DepartmentRoleDto dto) {
        DepartmentRole departmentRole = dozerService.map(dto, DepartmentRole.class);
        if(StringUtils.hasText(departmentRole.getDepartmentRoleId())){
            departmentRoleMapper.updateByPrimaryKeySelective(departmentRole);
        }else {
            departmentRoleMapper.insertSelective(departmentRole);
        }
        return departmentRole.getDepartmentRoleId();
    }

    @Override
    public PermissionCheckVo selectPermission(String permissionCode, String departmentRoleId, String empId) {
        return departmentRoleMapper.selectPermission(permissionCode,departmentRoleId,empId);
    }

    @Override
    public List<String> selectAdminIds(Boolean excludeDisabled) {
        return departmentRoleMapper.selectAdminIds(excludeDisabled);
    }

}
